// pages/sendInfo/sendInfo.js
const allUrl = require('../../config').allUrl
var app = getApp()
var userInfo = null
var openId = null


Page({
  data:{
    longitude: '',
    latitude: '',
    user_phone: ''
  },


  onLoad:function(options){
    userInfo = app.globalData.userInfo
    openId = app.globalData.openId

    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      longitude: options.longitude,
      latitude: options.latitude,
    })
    var that = this
    wx.getStorage({
      key: 'user_phone',
      success: function(res){
        that.setData({
          user_phone: res.data
        })
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    })

  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
    wx.hideToast()
  },



  //  点击按钮返回
  topBack:function(){
    wx.navigateBack({})
  },



  goback: function(complete){

    wx.navigateBack({
      delta: 1,
      complete: function(res){
        complete()
      }
    })
  },
  //  提交报警信息
  formSubmit: function(e){
    if (openId == null || openId.length < 2)
      return
    var inpu = e.detail.value
    var that = this


    if(inpu.usernumber.length != 11){
      app.showSomeModel('信息错误', '手机号码填写不正确', function(sucBack){}, function(errBack){}, function(complete){})
      return
    }else if(inpu.userinfo==null || inpu.userinfo.length<1){
      app.showSomeModel('信息错误', '备注填写不正确', function(sucBack){}, function(errBack){}, function(complete){})
      return
    }else if(that.data.longitude.length<1 || that.data.latitude.length<1){
      app.showSomeModel('信息错误', '位置信息不完整', function(sucBack){}, function(errBack){}, function(complete){})
      return
    }
    

    wx.showToast({
      title: "正在发送报警信息",
      icon: "loading",
      duration: 10000
    })

    //  发送报警信息
    wx.request({
      url: allUrl + 'Api/postCall.html',
      header: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: openId,
        nickname: userInfo.nickName,
        headimg: userInfo.avatarUrl,
        lng: that.data.longitude,
        lat: that.data.latitude,
        mobile: that.data.user_phone,
        remark: inpu.userinfo
      },
      method: 'POST', 
      success: function(res){
        console.log(res)
        if(res.data.status_code == 200){
            wx.showToast({
              title: '报警成功,请等待警察处理'
            })
            console.log('res.data.data.call_id:' + res.data.data.call_id)
            app.globalData.call_id = res.data.data.call_id
            //  发送成功之后，保存用户手机号
            wx.setStorage({
              key: 'user_phone',
              data: inpu.usernumber,
              complete: function(res) {
                // 拨打电话成功，返回上一级界面
                wx.showModal({
                  title: '提示',
                  content: '报警信息已提交，请拨打110报警电话',
                  showCancel: false,
                  confirmText: '拨打',
                  success: function (res) {
                    if (res.confirm) {
                      that.callPhone(function () { })
                    }
                  },
                  complete: function (res) { 
                    that.goback(function () {

                    })
                  }
                })
                
              }
            })
        }else{
          //  请求出问题。。。。。。。。
          if(res.data.msg){
            app.showSomeModel1('提示', res.data.msg,
              function () { }, function () { }, function () { })
          } else {
            app.showSomeModel1('提示', '报警失败',
              function () { }, function () { }, function () { })
          }
        }
      },
      fail: function (res) {
        app.showSomeModel1('提示', '报警失败',
          function () { }, function () { }, function () { })
      },
      complete: function(res) {}
    })
  },
  //  记录手机号输入
  inputAction: function(e){
    this.setData({
      user_phone: e.detail.value
    })
  },
  // 拨打电话
  callPhone: function(complete){
    var that = this
    wx.makePhoneCall({
      phoneNumber: '110',
      success: function(res) {
      },
      complete: function(res){
        complete()
      }
    })
  }


})